package moodish.client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;



import moodish.comm.*;
import moodish.comm.component.*;



public class ClientGUI{

	// Global data
	private  Object[][] data = {};
	private  String[] columnNames = { "Nickname", "Mood" , "Type"};

	private  ClientComm clientComm = new ClientCommComponent();
	private  JPanel actionPanel_s = new JPanel();
	private  JPanel actionPanel_n = new JPanel();
	private  JPanel actionPanel_c = new JPanel();
	private  JFrame guiFrame = new JFrame();
	private  JTextField text = new JTextField(25);
	private  DefaultTableModel dtm = new DefaultTableModel(data,
			columnNames);
	private  JTable jtable = new JTable(dtm);
	private  String username = "";
	private  ClientSideMessage clientSideMessage;

	public void main(String[] args) {
		
		//ServerMain server = new ServerMain;
		//ServerCommComponent serverComm  = new ServerCommComponent();
		//serverComm.start();
		
		// GUI construction
		guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		guiFrame.setTitle("Moodish Client  -  Asterisk");
		guiFrame.setSize(620, 600);
		guiFrame.setLocationRelativeTo(null);
		guiFrame.add(actionPanel_n, BorderLayout.NORTH);
		guiFrame.add(actionPanel_c, BorderLayout.CENTER);
		
		jtable.getColumnModel().getColumn(0).setPreferredWidth(100);
		jtable.getColumnModel().getColumn(1).setPreferredWidth(300);
		jtable.getColumnModel().getColumn(2).setPreferredWidth(200);
		
		guiFrame.add(actionPanel_s, BorderLayout.SOUTH);
		
		// options button
		JButton btnConnect = new JButton("Connect Server");
		JButton btnDisconnect = new JButton("Disconnect Server");
	
		actionPanel_n.add(btnConnect);
		actionPanel_n.add(btnDisconnect);
	

		btnConnect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!text.getText().isEmpty()) {
					try {
						clientComm.connect("localhost", text.getText());
					} catch (UnknownHostException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					JOptionPane.showMessageDialog(null, "Connected to server");
					username = text.getText();
					text.setText("");
					guiFrame.setTitle("Moodish Client - WELCOME "+username);
				} else {
					JOptionPane.showMessageDialog(null, "TextField is null!");
				}
			}
		});

		btnDisconnect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				clientComm.disconnect();
				JOptionPane.showMessageDialog(null, "Disconnect from server!");
			}
		});

		
		
		
		
		JButton btnFriend = new JButton("Friend");
		JButton btnUnfriend = new JButton("Unfriend");
		JButton btnSendmsg = new JButton("Send Message");

		actionPanel_s.add(text);
		actionPanel_s.add(btnFriend);
		actionPanel_s.add(btnUnfriend);
		actionPanel_s.add(btnSendmsg);

		btnFriend.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!text.getText().isEmpty()) {
					System.out.println("Client GUI: friendship enviado para "+text.getText());
					clientComm.friendship(text.getText());
					JOptionPane.showMessageDialog(null, "Friendship send!");
					text.setText("");
				} else {
					JOptionPane.showMessageDialog(null, "TextField is null!");
				}

			}
		});

		btnUnfriend.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (!text.getText().isEmpty()) {
					clientComm.unfriendship(text.getText());
					JOptionPane.showMessageDialog(null, "Unfriendship done!");
					text.setText("");
				} else {
					JOptionPane.showMessageDialog(null, "TextField is null!");
				}
			}
		});

		btnSendmsg.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!text.getText().isEmpty()) {
					clientComm.sendMoodishMessage(text.getText());
					//dtm.addRow(new Object[] { username, text.getText() });
					text.setText("");
				} else {
					JOptionPane.showMessageDialog(null, "TextField is null!");
				}
			}
		});

		actionPanel_c.add(jtable);
		
		guiFrame.setVisible(true);
		dtm.addRow(new Object[] { "Nickname", "Moodish Messge" , "Type" });
		for (;;) {
//			System.out.println("clientcom tem nova mensagem");
			if (clientComm.hasNextMessage()) {
				System.out.println("clientcom tem nova mensagem");
				clientSideMessage = clientComm.getNextMessage();
				dtm.addRow(new Object[] {
						clientSideMessage.getSendersNickname(),
						clientSideMessage.getPayload(),
						clientSideMessage.getType() });
			}
		}
	}
}